package com.school.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.TimerTask;

import javax.servlet.ServletContext;

public class DelFileTask extends TimerTask{ 
	
    private static boolean isRunning = false;      
      
    private ServletContext context = null;      
      
    public DelFileTask() {    
        super();    
    }    
        
    public DelFileTask(ServletContext context) {      
        this.context = context;      
    }      
    @Override    
    public void run() {    
            
        if (!isRunning) {
        	try {
				this.load(
						  	"http://127.0.0.1:8080/zhxy/updateDeviceStateByEndTime",
				          "floor=first&year=2017&month=9&isLeader=N");
			} catch (Exception e) {
				e.printStackTrace();
			}
           
            isRunning = false;  
        } else {      
            context.log("上一次任务执行还未结束");      
        }    
    } 
    public String load(String url,String query) throws Exception {
        URL restURL = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();
        //请求方式
        conn.setRequestMethod("GET");
        //设置是否从httpUrlConnection读入，默认情况下是true; httpUrlConnection.setDoInput(true);
        conn.setDoOutput(true);
        //allowUserInteraction 如果为 true，则在允许用户交互（例如弹出一个验证对话框）的上下文中对此 URL 进行检查。
        conn.setAllowUserInteraction(false);
        PrintStream ps = new PrintStream(conn.getOutputStream());
        ps.print(query);
        ps.close();
        BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line,resultStr="";
       /* while(null != (line=bReader.readLine())){
        	resultStr +=line;
        }*/
        bReader.close();
        return resultStr;

    }
    
    
    
}  